import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

/**
*The <code>InventoryGUI</code> class provides a GUI front end for managing data and implements
* the InventoryFile and Inventory classes.
* @author Thomas Pierson (Team 2)
* @version 1.0
**/
public class InventoryGUI
{
	private InventoryFile db;
	private String s = "by Thomas Pierson";
	private JPanel p;
	private AddPanel ap;
	private DrawingPanel dp;
	private ReportPanel rp;
	private JTextArea reportTA;
	private int printChoice = 0;
	private JTable table;

/**
*CreateMenuBar() returns a JMenuBar containing the menu choices.
**/
	private JMenuBar createMenuBar()
	{
		JMenuBar menuBar = new JMenuBar();
		JMenu menu;
		JMenuItem menuItem;

		ButtonListener listener = new ButtonListener();

		//Create the menus in the menu bar
		menu = new JMenu("Menu");
		menu.setMnemonic(KeyEvent.VK_M);
		menuBar.add(menu);

			menuItem = new JMenuItem("Add Item");
			menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_A, ActionEvent.ALT_MASK));
			menuItem.addActionListener(listener);
			menu.add(menuItem);

			menuItem = new JMenuItem("Delete Item");
			menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_D, ActionEvent.ALT_MASK));
			menuItem.addActionListener(listener);
			menu.add(menuItem);
		
		//Create Print menu
		menu = new JMenu("Print");
		menu.setMnemonic(KeyEvent.VK_P);
		menuBar.add(menu);

			menuItem = new JMenuItem("Display Inventory");
			menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_I, ActionEvent.ALT_MASK));
			menuItem.addActionListener(listener);
			menu.add(menuItem);

		menuBar.add(Box.createHorizontalGlue());

		//Build the File menu in the menu bar.
        	menu = new JMenu("File");
        	menu.setMnemonic(KeyEvent.VK_F);
        	menuBar.add(menu);

            		menuItem = new JMenuItem("Quit");
            		menuItem.setAccelerator(KeyStroke.getKeyStroke(
                		KeyEvent.VK_Q, ActionEvent.ALT_MASK));
            		menuItem.addActionListener (listener);
            		menu.add(menuItem);
	
		return menuBar;
	}

/**
* ButtonListener is an inner class of EmpApp. It watches for action events
* to happen. These include buttons being clicked and keys being pressed.
* The main purpose of this code is to process menu selections.
**/
	private class ButtonListener implements ActionListener
	{

		public void actionPerformed(ActionEvent e)
		{
			JMenuItem source = (JMenuItem)(e.getSource());
			CardLayout cl = (CardLayout)(p.getLayout());

			if(source.getText().equals("Add Item"))
			{
  				cl.show(p, "Add Item Panel");
			}
			else if(source.getText().equals("Delete Item"))
			{
				cl.show(p, "Report Panel");
                		deleteItem();

			}
			else if(source.getText().equals("Display Inventory"))
			{
				//PrintUtilities.printComponent(reportTA);
				cl.show(p, "Report Panel");
				displayReport(1);

			}
                        else if (source.getText().equals("Quit"))
            	        {
              			System.exit(0);
	                }
			
			else
			{
				cl.show(p, "Drawing Panel");
			}


		}
	}

/**
*The <code>AddPanel</code> inner class will sit on the main panel p.  It is the
*screen that is shown when Add Item is selected.
**/
	class AddPanel extends JPanel
        {
   	     	private JLabel itemNumLabel, manufacturerLabel, sizeLabel, priceLabel, quantityLabel, blankLabel;
        	private JTextField itemNumTF, manufacturerTF, sizeTF, priceTF, quantityTF;
        	private JButton addItemBtn;
        	private JPanel jp;

        	AddPanel()
        	{
            		JPanel jp = new JPanel(new SpringLayout());

            		itemNumLabel = new JLabel ("Item Number:", JLabel.TRAILING);
            		jp.add (itemNumLabel);
            		itemNumTF = new JTextField (8);
            		jp.add (itemNumTF);

            		manufacturerLabel = new JLabel ("Manufacturer:", JLabel.TRAILING);
            		jp.add (manufacturerLabel);
            		manufacturerTF = new JTextField (8);
            		jp.add (manufacturerTF);

            		sizeLabel = new JLabel ("Size:", JLabel.TRAILING);
            		jp.add (sizeLabel);
            		sizeTF = new JTextField (8);
            		jp.add (sizeTF);  

			priceLabel = new JLabel ("Price:", JLabel.TRAILING);
            		jp.add (priceLabel);
            		priceTF = new JTextField (8);
            		jp.add (priceTF);  

			quantityLabel = new JLabel ("Quantity:", JLabel.TRAILING);
            		jp.add (quantityLabel);
            		quantityTF = new JTextField (8);
            		jp.add (quantityTF);  

            		// We need this blank label to keep an even number of fields.
            		// Without it I get a runtime error from SpringUtilities.
            		blankLabel = new JLabel("");
            		jp.add (blankLabel);

            		addItemBtn = new JButton ("Add Item");
            		ButtonListener listener = new ButtonListener();
            		addItemBtn.addActionListener (listener);
            		jp.add (addItemBtn);

            		jp.setPreferredSize (new Dimension(400, 200));
            		jp.setBackground (Color.yellow); 
            		add (jp);

            		SpringUtilities.makeCompactGrid(jp,
                                            		6, 2,       //rows, cols
                                            		30, 10,     //initX, initY
                                            		10, 10);    //xPad, yPad
        	}

/**
*This ButtonListener is an inner class of AddPanel. It watches for action
*events to happen. These include buttons being clicked and keys being 
*pressed. The main purpose of this code is to process the Add Item
*button click.
**/
        	private class ButtonListener implements ActionListener
        	{
            		public void actionPerformed (ActionEvent event)
            		{
                		if (event.getSource() == addItemBtn)
                		{
                    			int itemNum = 0;
                    			String input = itemNumTF.getText();

                    			if (input == null) {
                        			return;
                    			}
                    			else if (input.equals("")) {
                        			return;
                    			}
                    			else {
                        			itemNum = Integer.parseInt(input);                 
                        			db.addItem( itemNum, manufacturerTF.getText(), sizeTF.getText(), Double.parseDouble(priceTF.getText()), Integer.parseInt(quantityTF.getText()));
                        			itemNumTF.setText("");
                        			manufacturerTF.setText("");
                        			sizeTF.setText("");
						priceTF.setText("");
						quantityTF.setText("");
                    			}
                		}
            		}
        	}
    	}

/**
*The <code>DrawingPanel</code> inner class will sit on top of the main
*panel p. It is used as the splash screen when the application starts up.
**/
	class DrawingPanel extends JPanel
	{
		DrawingPanel()
		{
			setBackground(Color.white);
			ImageIcon icon = new ImageIcon("USBdrive.jpg");
			JLabel label = new JLabel("<html><font size=5>Inventory<br>Management<br>System</font></html>",icon, JLabel.LEFT);
			add(label);
		}
	}

/**
*The <code>ReportPanel</code> inner class will sit on top of the main
*panel p. It is placed on top for many of the main menu options including
*Reports and Delete. It provides a simple scrollable text area to display information.
**/
	class ReportPanel extends JPanel
	{
		private JScrollPane reportSP;
		private String s = "";

		ReportPanel()
		{
			reportTA = new JTextArea(s, 14, 32);
			reportTA.setEditable(false);
			reportSP = new JScrollPane(reportTA);

			setBackground(new Color(205, 215, 235));
			JLabel label = new JLabel("<html><font size=3>Item Number: Manufacturer, Size, Price, Quantity</font></html>");
			add(label);
			
			add(reportSP);
		}
	}

/**
*displayReport() displays the item data in the report panel's text area 
**/
	public void displayReport(int rType)
	{
		reportTA.setText(db.toString());
	}

/**
*deleteItem() deletes an inventory record given an item number. It 
*makes use of two different types of dialog boxes and displays the 
*results in the report panel's text area.
**/
	public void deleteItem()
    	{
        	String input = "", results = "";

	        input = JOptionPane.showInputDialog("Enter Item Number");

	        if (input == null) {
        	 	return;
        	}
        	else if (input.equals("")) {
            		results = "ItemNum not entered";
        	}
        	else {
            		results = db.searchItemNum(Integer.parseInt(input));

            		if (results.equals("")) {
                	results = "No Match Found for " + input;
            		} 
            		else {
                		Object[] options = { "OK", "CANCEL" };
                		int choice = JOptionPane.showOptionDialog(null, 
                    			("Click OK to delete " + results), "Warning", 
                    			JOptionPane.DEFAULT_OPTION, JOptionPane.WARNING_MESSAGE,
                    			null, options, options[0]);

                		if (choice == 0) {
                    			db.deleteItem(Integer.parseInt(input));
                    			results += " has been deleted";
                		} else {
                    			results = "Action canceled";
                		}
            		}
        	}
        	reportTA.setText(results);
    	}

/**
*getMainPanel() is a getter method that returns the main panel
*@return p A <code>JPanel</code> that is the main panel of this application
**/
	public JPanel getMainPanel()
	{
		return p;
	}

/**
* This is the constructor for the InventoryGUI application. It creates each of the 
* user interface panels and adds them to a stacked card layout on the main panel.
**/
	public InventoryGUI()
	{
		db = new InventoryFile();
		p = new JPanel();
		p.setLayout(new CardLayout());

		dp = new DrawingPanel();
		rp = new ReportPanel();
		ap = new AddPanel();
	
		p.add(dp, "Drawing Panel");
		p.add(rp, "Report Panel");
		p.add(ap, "Add Item Panel");
	}

/**
*Create the GUI and show it.  For thread safety, this method should be 
*invoked from the event-dispatching thread.
**/
	private static void createAndShowGUI()
	{
		JFrame frame = new JFrame("Inventory Management System 1.0");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		InventoryGUI ig = new InventoryGUI();
		frame.setJMenuBar(ig.createMenuBar());
		frame.setContentPane(ig.getMainPanel());

		frame.setSize(500, 400);
		frame.setVisible(true);
	}

	public static void main(String[] args)
	{
		javax.swing.SwingUtilities.invokeLater(new Runnable() { public void run(){ createAndShowGUI(); } });
	}
}			
